Thread: string char * and char []

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715

    string char * and char []

    I'm coding a simple program that uses strings. I have two choices
    to define string like char string[1024] taht is to assume max size of string then ask user to enter his own string from keyboard and then gets(string);
    But what if I use char *string; and then gets(string);
    Is this method dangerous and is there a chance if user enter a long string to overwrite some memory and cause program to crash? I experimented with this in Turbo C and everything seems to work fine. But....?
    If someone can answer this I would appreciated
    Thanks

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Read this: Why gets() is bad / Buffer Overflows
    And this: Get a line of text from the user/keyboard

    Post if you have any questions.

    gg

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > experimented with this in Turbo C and everything seems to work fine. But....?
    Yeah, that's DOS for you.

    Your pointers can be left uninitialised pointing at who knows where in memory, and for a short while it's pretty much guaranteed that it will appear to work.

    But as time passes, your program gets bigger and you trash more and more memory, sooner or later you'll come unstuck big-time.

    Get a better OS and a better compiler, one which will kill your program at the first errant memory access.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Salem
    > experimented with this in Turbo C and everything seems to work fine. But....?
    Yeah, that's DOS for you.
    There's no mention of DOS. Turbo C 1.0 works just fine up into in XP....
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    It's still a DOS program working in an unprotected address space of 1MB
    Running under XP doesn't change that
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    I have win 98 and access turbo C under command prompt, so
    simple and clear: using char * is not recommended???

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Sure you can use a char*
    But you've got to remember to allocate some memory before you use it

    char buff[BUFSIZ];
    fgets( buff, BUFSIZ, stdin ); // check return

    char *buff = malloc( BUFSIZ ); // check return
    fgets( buff, BUFSIZ, stdin ); // check return


    What you can never do (with any pointer) is simply use it without allocating any memory, like so
    char *buff;
    fgets( buff, BUFSIZ, stdin ); // BAD, very BAD
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    23
    hi,

    my understanding is that if you are going to use char *pt, make sure you initialise pt to something

    e.g.
    char *pt;
    char array[10];

    pt = array;

    or... pt = malloc(...);

    I have forgotten how to use malloc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. String Class Operations
    By Aidman in forum C++ Programming
    Replies: 10
    Last Post: 04-06-2003, 02:29 PM
  4. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM